package com.rxst.msg.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.rxst.msg.base.ResponseVo;
import com.rxst.msg.dto.save.MsgxEquipmentSaveDto;
import com.rxst.msg.dto.search.MsgxEquipmentSearchDto;
import com.rxst.msg.dto.update.MsgxEquipmentUpdateDto;
import com.rxst.msg.entity.MsgxEquipmentEntity;
import com.rxst.msg.server.MsgxEquipmentService;
import com.rxst.msg.utils.AESUtils;
import com.rxst.msg.utils.MacUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * 设备授权表
 *
 * @author rxst
 * @email rxst@gmail.com
 * @date 2021-08-19 11:47:00
 */
@RestController
@RequestMapping("equipment")
@Api(value = "设备授权表", tags = "设备授权表")
@Slf4j
public class MsgxEquipmentController {

    @Autowired
    private MsgxEquipmentService service;

    /**
     * 列表
     */
    @PostMapping("/list")
    @ApiOperation("列表")
    public ResponseVo List(@RequestBody MsgxEquipmentSearchDto dto) {
        return ResponseVo.ok(service.list(dto));
    }

    /**
     * 分页Function
     */
    @PostMapping("/page")
    @ApiOperation("分页")
    public ResponseVo page(@RequestBody MsgxEquipmentSearchDto dto) {
        return ResponseVo.ok(service.page(dto));
    }

    /**
     * 详情
     */
    @GetMapping("/info")
    @ApiOperation("详情")
    public ResponseVo info(String id) {
        return ResponseVo.ok(service.getById(id));
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("保存")
    public ResponseVo save(@RequestBody MsgxEquipmentSaveDto dto) {
        //校验类型
        MsgxEquipmentEntity entity = BeanUtil.toBean(dto, MsgxEquipmentEntity.class);
        return ResponseVo.ok(service.saveEntity(entity));
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("修改")
    public ResponseVo update(@RequestBody MsgxEquipmentUpdateDto dto) {
        MsgxEquipmentEntity entity = BeanUtil.toBean(dto, MsgxEquipmentEntity.class);
        return ResponseVo.ok(service.updateEntity(entity));
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete")
    @ApiOperation("批量删除")
    public ResponseVo delete(@RequestParam(value = "ids") List<Long> ids) {
        if (CollUtil.isNotEmpty(ids)) {
            service.removeByIds(ids);
        } else {
            return ResponseVo.error("参数不能为空");
        }
        return ResponseVo.ok();
    }

    /**
     * 批量逻辑删除
     */
    @DeleteMapping("/delete/logic")
    @ApiOperation("批量逻辑删除")
    public ResponseVo deleteLogic(@RequestParam(value = "ids") List<Long> ids) {
        if (CollUtil.isNotEmpty(ids)) {
            service.deleteLogicByIds(ids);
        } else {
            return ResponseVo.error("参数不能为空");
        }
        return ResponseVo.ok();
    }

    @GetMapping("/down")
    @ApiOperation("下载授权文件")
    public void downFile(String id, HttpServletResponse response) {
        try {
            // 设置相关格式
            response.setContentType("application/force-download");
            // 设置下载后的文件名以及header
            response.addHeader("Content-disposition", "attachment;fileName=auth.key");
            // 创建输出对象
            OutputStream os = response.getOutputStream();
            StringBuffer buffer = MacUtil.getMachineCode();
            MsgxEquipmentEntity equipmentEntity = service.getById(id);
            if (equipmentEntity != null) {
                // 4 授权开始时间
                buffer.append(DateUtil.format(equipmentEntity.getEquipmentAuthStartTime(), "yyyy-MM-dd HH:mm:ss") + ",");
                // 5 授权结束时间
                buffer.append(DateUtil.format(equipmentEntity.getEquipmentAuthEndTime(), "yyyy-MM-dd HH:mm:ss") + ",");
                // 6 设备mac地址
                buffer.append(equipmentEntity.getEquipmentMac() + ",");
            }
            // 7 双重加密 授权key
            String key = "rxst-msg";
            String keyEncrypt = AESUtils.aesEncrypt(key);
            String keyDecrypt = AESUtils.aesDecrypt(keyEncrypt);
            buffer.append(keyEncrypt);
            System.out.println(keyDecrypt);
            // 协议加密
            System.out.println(buffer.toString());
            String aesEncrypt = AESUtils.aesEncrypt(buffer.toString());
            String aesDecrypt = AESUtils.aesDecrypt(aesEncrypt);
            System.out.println("协议加密" + aesEncrypt);
            System.out.println("协议解密" + aesDecrypt);
            // 常规操作
            byte[] bytes = aesEncrypt.getBytes();
            os.write(bytes, 0, bytes.length);
            os.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
